package com.itaem.community.modules.infrastructure.authority.service.permission.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itaem.community.core.exception.data.NoSuchDataException;
import com.itaem.community.modules.infrastructure.authority.dto.permission.PermissionDTO;
import com.itaem.community.modules.infrastructure.authority.entity.permission.PermissionEntity;
import com.itaem.community.modules.infrastructure.authority.entity.role.RolePermissionEntity;
import com.itaem.community.modules.infrastructure.authority.mapper.permission.PermissionDAO;
import com.itaem.community.modules.infrastructure.authority.mapper.permission.PermissionTransformer;
import com.itaem.community.modules.infrastructure.authority.mapper.role.RoleDAO;
import com.itaem.community.modules.infrastructure.authority.mapper.role.RolePermissionDAO;
import com.itaem.community.modules.infrastructure.authority.mapper.role.RoleTransformer;
import com.itaem.community.modules.infrastructure.authority.service.permission.PermissionService;
import org.omg.CORBA.PolicyError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionDAO, PermissionEntity> implements PermissionService {

    @Autowired
    private PermissionDAO permissionDAO;

    @Autowired
    private RolePermissionDAO rolePermissionDAO;

    @Autowired
    private PermissionTransformer permissionTransformer;

    @Autowired
    private RoleDAO roleDAO;

    @Autowired
    private RoleTransformer roleTransformer;

    @Override
    public PermissionDTO findPermissionById(Integer permissionId) {
        return permissionTransformer.toDto(
                // 获取权限
                Optional.ofNullable(permissionDAO.selectById(permissionId))
                        // 不存在则抛出异常
                        .orElseThrow(() -> new NoSuchDataException("不存在此权限")),
                // 查询绑定了这个权限的角色
                roleDAO.selectBatchIds(
                        rolePermissionDAO.selectList(
                                new QueryWrapper<RolePermissionEntity>()
                                        .lambda()
                                        .eq(RolePermissionEntity::getPermissionId, permissionId))
                        .stream()
                        .map(RolePermissionEntity::getRoleId)
                        .collect(Collectors.toList())
                )
                        .stream()
                        .map(roleTransformer::entityToBriefDto)
                        .collect(Collectors.toList())
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removePermissionById(Integer permissionId) {
        // 先删除权限与角色的链接
        rolePermissionDAO.delete(
                new QueryWrapper<RolePermissionEntity>()
                        .lambda()
                        .eq(RolePermissionEntity::getPermissionId,
                                // 查询权限是否存在
                                Optional.ofNullable(permissionDAO.selectById(permissionId))
                                        .orElseThrow(() -> new NoSuchDataException("不存在此权限"))
                                        // 存在则取出id执行关联删除
                                        .getId())
        );
        // 删除权限本身
        removeById(permissionId);
        return Boolean.TRUE;
    }
}
